/**
 * This file is released under the GNU General Public License.
 * Refer to the COPYING file distributed with this package.
 *
 * Copyright (c) 2008-2009 WURFL-Pro srl
 */
package net.sourceforge.wurfl.core.request.normalizer.generic;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.sourceforge.wurfl.core.request.normalizer.UserAgentNormalizer;

import org.apache.commons.lang.Validate;

/**
 * The UPLink {@link UserAgentNormalizer}.
 * 
 * <p>
 * Strips out the trailing "UP.Link*" from the target user-agent.
 * </p>
 * 
 * @author WURFL-PRO SRL, Rome, Italy
 * @version $Id: UPLinkNormalizer.java 1045 2009-03-09 15:52:53Z filippo.deluca $
 */
public class UPLinkNormalizer implements UserAgentNormalizer {

	/** The regex pattern of trailing "UP.Link" String */
	private static final Pattern upLinkPattern = Pattern
			.compile("\\s*UP.Link.+");

	/**
	 * Replace the trailing "UP.Link*" with "".
	 */
	public String normalize(String userAgent) {
		Validate.notNull(userAgent, "The userAgent is null");
		String normalizedUserAgent = userAgent;

		Matcher matcher = upLinkPattern.matcher(normalizedUserAgent);

		if (matcher.find()) {
			normalizedUserAgent = matcher.replaceAll("");
		}

		return normalizedUserAgent;
	}

}
